package com.yygo.service;

import com.yygo.configentity.attachment.Role;
import com.yygo.configentity.attachment.RoleAttachment;
import com.yygo.configentity.attachment.RoleAttachments;
import com.yygo.util.RoleAttachmentConfigUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by huanli on 2015/11/14.
 */
public class RoleAttachmentsConfigurer {
    private RoleAttachments roleAttachments;

    public RoleAttachment getPhase(String role, String phase, String status) {
        List<Role> roleList = roleAttachments.getRoles().stream().filter((attachmentRole)-> attachmentRole.getName().equals(role)).collect(Collectors.toList());
        if (roleList.size()>0){
            List<RoleAttachment> retAttachments = roleList.get(0).getRoleAttachments().stream().filter((roleAttachment) -> roleAttachment.hasPhase(phase) && roleAttachment.getStatus().equals(status)).collect(Collectors.toList());
            if (retAttachments.size()>0)
                return retAttachments.get(0);
        }
        return null;
    }

    public void init() {
        roleAttachments = new RoleAttachmentConfigUtil().read();
    }
}
